package com.sl.carcode.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.sl.carcode.R;
import com.sl.carcode.activity.DetailActivity;
import com.sl.carcode.activity.ShopActivity;
import com.sl.carcode.adapter.DemoAdapter;
import com.sl.carcode.adapter.GoodLVAdapter;
import com.sl.carcode.bean.GoodInfo;
import com.sl.carcode.utils.GsonUtils;
import com.sl.carcode.utils.ImageUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by  raysh  on 2017/11/14.
 */

public class ChatFragment extends android.support.v4.app.Fragment {

    private ArrayList<GoodInfo> list;
    private DemoAdapter adapter;
    private RecyclerView rl_shapLiet;

    public ChatFragment() {
        super();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EventBus.getDefault().register(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getData();
        View view = inflater.inflate(R.layout.fragment_chat, null);
        rl_shapLiet = (RecyclerView) view.findViewById(R.id.rl_shapList);
        rl_shapLiet.setHasFixedSize(true);
        rl_shapLiet.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        adapter = new DemoAdapter(rl_shapLiet);

        adapter.setOnItemClickedListener(new DemoAdapter.OnItemClickedListener() {
            @Override
            public void onItemClick(int position) {
                Log.i("sl", "点击了，跳转" + position);
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("Good", list.get(position));
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(int position) {
                return false;
            }
        });
        rl_shapLiet.setAdapter(adapter);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View getView() {
        return super.getView();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public ArrayList<GoodInfo> getData() {
        list = new ArrayList<>();
        OkHttpClient okHttpClient = new OkHttpClient();
//        String url = "http://api.dataoke.com/index.php?r=goodsLink/www";
//        url += "&type=www_quan" + "&appkey=7534a6b097" + "&v=2&page=10";
        String url = "http://api.dataoke.com/index.php?r=Port/index";
        url += "&type=top100" + "&appkey=7534a6b097" + "&v=2&page=10";
        Request request = new Request.Builder().url(url).get().build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject jo = jsonObject.getJSONObject("data");
                        Log.i("sl", "data格式" + jo.toString());
                        JSONArray jsonArray = jsonObject.getJSONArray("result");
                        Log.i("sl", "result格式" + jsonArray.toString());
                        list = GsonUtils.jsonArray2List(jsonArray.toString(), GoodInfo.class);
//                        ArrayList<GoodInfo> arrayListlist = GsonUtils.jsonArray2List(jsonArray.toString(), GoodInfo.class);
//                        for (int i = 0; i < 20; i++) {
//                            list.add(arrayListlist.get(i));
//                        }
                        rl_shapLiet.post(new Runnable() {
                            @Override
                            public void run() {
                                adapter.replaceAll(list);
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.i("sl", result);
                }
            }
        });
        return list;
    }


//    public ArrayList<String> getData() {
//        ArrayList<String> list = new ArrayList<>();
//        for (String url : ImageUtil.imageUrls) {
//            list.add(url);
//        }
//        return list;

    public void login(View view, final Intent intent) {
        final AlibcLogin alibcLogin = AlibcLogin.getInstance();

        alibcLogin.showLogin(new AlibcLoginCallback() {

            @Override
            public void onSuccess(int i) {
                Toast.makeText(getContext(), "登录成功 ",Toast.LENGTH_LONG).show();
                startActivity(intent);
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(), "登录失败 ",Toast.LENGTH_LONG).show();
            }
        });
    }
}

